(function(){
    var util = require("wap_common:widget/ui/libs/util.js").util,
        $page = $(".main-login"),
        $username = $page.find(".user-name"),
        $loginBtn = $page.find(".login-btn"),
        $tip = $page.find(".tip"),
        $openId = $page.find("input[name=open_id]"),
        $password = $page.find(".password");

    function check(){
        return util.checkPassword($password.val()) &&
            util.checkPhone($username.val());
    }

    $page.find(".user-name,.password").on("input change",function(){
        if(check()){
            $loginBtn.addClass("btn-primary");
        }else{
            $loginBtn.removeClass("btn-primary");
        }
    });

    $loginBtn.on("click",function(){
        $tip.css("visibility","hidden");
        if(check()){
            $.post('/account/loginuser',{
                'user_name': $.trim($username.val()),
                'passwd': $.trim($password.val())
//                'open_id': $.trim($openId.val())
            }, function(data) {
                if(data && data.error_no == 0) {
                    var from = '/wap/wallet';
                    if(util.getUrlParams()["zrb_from"]){
                        from = util.getUrlParams()["zrb_from"];
                    }
                    location.href = from;
                    if($("#remember-password").attr("checked")){
                        localStorage.setItem("zhengrongbao_user_name",$.trim($username.val()))
                    }else{
                        localStorage.removeItem("zhengrongbao_user_name")
                    }
                } else {
                    $tip.css("visibility","visible").text(data.error_message);
                }
            },'json');
        }
    });

    (function(){
        if(localStorage.getItem("zhengrongbao_user_name")){
            $username.val(localStorage.getItem("zhengrongbao_user_name"))
        }
    })();
})();